package com.navinf.parkingnavi.util;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.navinfo.parkingnavi.R;
import com.navinfo.parkingnavi.fragment.BaseFragment;
import com.navinfo.parkingnavi.fragment.PoiInfoFragment;
import com.navinfo.parkingnavi.fragment.PoiListFragment;
import com.navinfo.parkingnavi.fragment.RouteInfoFragment;
import com.navinfo.parkingnavi.fragment.RoutePanningFragment;
import com.navinfo.parkingnavi.fragment.SearchFragment;
import com.navinfo.parkingnavi.fragment.SelectMapPoiFragment;

import java.util.ArrayList;
import java.util.List;

/*
 *com.navinfo.parkingnavi.fragment
 *zhjch
 *2020/5/12
 *11:20
 *说明（fragment 管理）
 */
public class FragmentsManager {

    private FragmentManager mFragmentManager;
    private FragmentActivity mActivity;
    private static volatile FragmentsManager mInstance;
    private List<BaseFragment> fragmentList = new ArrayList<BaseFragment>();


    public static FragmentsManager getInstance() {

        if (mInstance == null) {
            synchronized (FragmentsManager.class) {
                if (mInstance == null) {
                    mInstance = new FragmentsManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化
     *
     * @param activity
     */
    public void init(@NonNull FragmentActivity activity) {
        mActivity = activity;
        mFragmentManager = activity.getSupportFragmentManager();

    }

    /**
     * 销毁
     */
    public void onDestroy() {
        mInstance = null;
    }

    /**
     * 显示搜索页面
     */
    public void showSearchFragment(Bundle bundle, BaseFragment.OnBaseFragmentListener listener) {
        SearchFragment fragment = new SearchFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        showFragment(fragment, true, listener);
    }

    /**
     * 显示Fragment
     *
     * @param bRemoveOther 是否移除其他fragment
     * @param fragment
     * @param listener
     */
    private void showFragment(BaseFragment fragment, boolean bRemoveOther, BaseFragment.OnBaseFragmentListener listener) {
        if (fragment == null)
            return;
        mActivity.findViewById(R.id.main_fragment).setVisibility(View.VISIBLE);
        if (bRemoveOther) {
            removeAllFragments();
        } else {
            hideAllFragments();
        }
        fragment.setOnBaseFragmentListener(listener);
        mFragmentManager.beginTransaction().add(R.id.main_fragment, fragment, fragment.getFragmentTag()).show(fragment).commit();
        fragmentList.add(fragment);
    }

    /**
     * 显示路径规划fragment
     *
     * @param bundle
     * @param listener
     */
    public void showRoutePlanningFragment(Bundle bundle, BaseFragment.OnBaseFragmentListener listener) {
        RoutePanningFragment fragment = new RoutePanningFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        showFragment(fragment, false, listener);
    }

    /**
     * 显示地图选点页面
     *
     * @param listener
     */
    public void showSelectMapFragment(BaseFragment.OnBaseFragmentListener listener) {
        hideAllFragments();
        SelectMapPoiFragment fragment = new SelectMapPoiFragment();
        showFragment(fragment, false, listener);
    }

    /**
     * 显示单个poi页面
     *
     * @param bundle
     * @param listener
     */
    public void showPoiInfoFragment(Bundle bundle, BaseFragment.OnBaseFragmentListener listener) {
        if (bundle == null)
            return;
        if (fragmentList.size() > 0) {
            BaseFragment fragmentLast = fragmentList.get(fragmentList.size() - 1);
            if (fragmentLast.getFragmentTag().equals(PoiInfoFragment.class.getName())) {
                fragmentLast.setArguments(bundle);
                fragmentLast.onDataChange(bundle);
                return;
            } else {
                hideAllFragments();
            }
        }
        PoiInfoFragment fragment = new PoiInfoFragment();
        fragment.setArguments(bundle);
        showFragment(fragment, false, listener);
    }

    /**
     * 显示poi搜索列表页面
     *
     * @param bundle
     * @param listener
     */
    public void showPoiListFragment(Bundle bundle, BaseFragment.OnBaseFragmentListener listener) {
        if (bundle == null)
            return;
        PoiListFragment fragment = new PoiListFragment();
        fragment.setArguments(bundle);
        showFragment(fragment, true, listener);
    }

    /**
     * 处理回退按钮
     *
     * @return false, activity 自己处理，true fragment 自己处理
     */
    public boolean onBackPressed() {

        if (fragmentList.size() > 0) {
            BaseFragment fragment = fragmentList.get(fragmentList.size() - 1);
            if (!fragment.onBackPressed()) {
                fragment.onHideOrRemove(true);
                mFragmentManager.beginTransaction().remove(fragment).commit();
                fragmentList.remove(fragment);
                if (fragmentList.size() > 0) {
                    BaseFragment fragment1 = fragmentList.get(fragmentList.size() - 1);
                    mFragmentManager.beginTransaction().show(fragment1).commit();
                } else
                    mActivity.findViewById(R.id.main_fragment).setVisibility(View.GONE);
            }
            return true;
        }
        return false;
    }

    /**
     * 返回上一个fragment，并传递数据
     *
     * @param bundle
     * @return
     */
    public boolean onBackPressed(Bundle bundle) {

        if (fragmentList.size() > 0) {
            BaseFragment fragment = fragmentList.get(fragmentList.size() - 1);
            if (!fragment.onBackPressed()) {
                fragment.onHideOrRemove(true);
                mFragmentManager.beginTransaction().remove(fragment).commit();
                fragmentList.remove(fragment);
                if (fragmentList.size() > 0) {
                    BaseFragment fragment1 = fragmentList.get(fragmentList.size() - 1);
                    mFragmentManager.beginTransaction().show(fragment1).commit();
                    if (bundle != null) {
                        fragment1.onDataChange(bundle);
                    }
                } else
                    mActivity.findViewById(R.id.main_fragment).setVisibility(View.GONE);
            }
            return true;
        }
        return false;
    }

    /**
     * 移除所有fragment
     */
    public void removeAllFragments() {
        while (fragmentList.size() > 0) {
            BaseFragment fragmentLast = fragmentList.get(0);
            fragmentLast.onHideOrRemove(true);
            fragmentList.remove(fragmentLast);
            mFragmentManager.beginTransaction().remove(fragmentLast).commit();
        }
    }

    /**
     * 隐藏所有fragment
     */
    private void hideAllFragments() {
        for (BaseFragment baseFragment : fragmentList) {
            baseFragment.onHideOrRemove(false);
            mFragmentManager.beginTransaction().hide(baseFragment).commit();
        }
    }

    public boolean bFragmentShow() {
        if(fragmentList.size() == 1 && fragmentList.get(0) instanceof PoiInfoFragment){
            return false;
        }
        if (fragmentList.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 显示路径规划详细信息
     *
     * @param bundle
     */
    public void showRouteInfoFragment(Bundle bundle, BaseFragment.OnBaseFragmentListener listener) {
        RouteInfoFragment fragment = new RouteInfoFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        showFragment(fragment, false, listener);
    }
}
